{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 08 - Markov Chain Monte Carlo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Code 8.1 Hastings Algorithm**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 182253,\n",
       " 2: 363624,\n",
       " 3: 542872,\n",
       " 4: 724022,\n",
       " 5: 906598,\n",
       " 6: 1089718,\n",
       " 7: 1272234,\n",
       " 8: 1455306,\n",
       " 9: 1640267,\n",
       " 10: 1823106}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "island_visit_counts = {i:0 for i in range(1, 11)}\n",
    "\n",
    "n_sims = 10**7\n",
    "current_island = 10\n",
    "\n",
    "for _ in range(n_sims):\n",
    "    if random.random() < .50:\n",
    "        proposal = (current_island - 1)\n",
    "    else:\n",
    "        proposal = (current_island + 1) \n",
    "        \n",
    "    \n",
    "    if proposal == 0:\n",
    "        proposal = 10\n",
    "    elif proposal == 11:\n",
    "        proposal = 1\n",
    "\n",
    "    p_move = proposal / current_island\n",
    "\n",
    "    if random.random() < p_move:\n",
    "        current_island = proposal\n",
    "\n",
    "    island_visit_counts[current_island] += 1\n",
    "    \n",
    "island_visit_counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
